using System.Collections;
using UnityEngine;
using UnityEngine.Events;

public class BottleController : MonoBehaviour
{
	public static bool blockAllBottlesController;

	public Material backgroundFluidMaterial;

	public Material foregroundFluidMaterial;

	public Vector3 endPosition;

	public Vector3 startPosition;

	protected float maxAngle = 10f;

	protected float minAngle = -9.7f;

	protected float inputOffset;

	protected float currentAngle;

	private float speed = 2f;

	private LiquidFallingController liquidFallingController;

	private bool startedFallingFluidAnimation;

	private Vector3 lastMousePosition;

	private float mouseMinMove;

	private float mouseMaxMove = 300f;

	private float mouseToleranceClick = 5f;

	private float minFluidWith = 0.15f;

	private GameObject prefabLiquidFalling;

	public bool enabledObject;

	private bool startedClick;

	private float slideInTime = 0.25f;

	private float slideOutTime = 0.25f;

	private float delaySlideOutTime = 0.5f;

	private GameObject bottleLiquid;

	private GameObject bottleLiquidStart;

	private Transform glassFront;

	private float blockPositionX = 160f;

	private float blockPositionXAfterScale = 160f;

	protected float lastAngle;

	private float offsetAngle;

	private float orginalOffsetAngle;

	private float currentOffsetAngle;

	private float botttleLiquidSpeedCurrentWave;

	private float divider = 1f;

	private float startingDivider = 1f;

	private float multiplierDivider = 1.2f;

	private float minBottleLiquidJump = 2f;

	private float maxBottleLiquidJump = 6f;

	private float minLiquidSpeed = 0.05f;

	private float maxLiquidSpeed = 0.3f;

	private float cuttoffAnimation = 0.1f;

	private BottleWaveState waveState = BottleWaveState.NULL;

	private UnityAction onBottleSlideOutAction;

	private bool slideInAnimation;

	protected virtual void Start()
	{
		prefabLiquidFalling = Resources.Load<GameObject>("Prefabs/LiquidFalling");
		bottleLiquid = base.transform.FindDeepChild("BottleLiquid").gameObject;
		bottleLiquidStart = base.transform.FindDeepChild("BottleLiquidStart").gameObject;
		string name = base.gameObject.name;
		LiquidType liquidType = LiquidTypes.GetLiquidType(base.gameObject.name.Split('_')[1]);
		LiquidColorData colorDataForLiquid = Globals.instance.liquidColorsDataManager.GetColorDataForLiquid(liquidType);
		if (colorDataForLiquid != null)
		{
			bottleLiquid.GetComponent<SpriteRenderer>().color = colorDataForLiquid.bottleLiquid;
			Shader shader = Shader.Find("UI/Default");
			backgroundFluidMaterial = new Material(shader);
			backgroundFluidMaterial.color = colorDataForLiquid.fallingLiquidBackground;
			foregroundFluidMaterial = new Material(shader);
			foregroundFluidMaterial.color = colorDataForLiquid.fallingLiquidForeground;
		}
		blockPositionXAfterScale = blockPositionX * UnityEngine.Object.FindObjectOfType<CanvasWorldSpaceResize>().scale.x;
	}

	protected void MouseInput()
	{
		if (Input.GetMouseButtonDown(0) && enabledObject && !blockAllBottlesController)
		{
			Vector3 mousePosition = UnityEngine.Input.mousePosition;
			if (mousePosition.x > blockPositionXAfterScale)
			{
				startedClick = true;
				lastMousePosition = UnityEngine.Input.mousePosition;
			}
		}
		if (Input.GetMouseButton(0) && enabledObject && !blockAllBottlesController && startedClick)
		{
			Vector3 mousePosition2 = UnityEngine.Input.mousePosition;
			float y = mousePosition2.y;
			float num = lastMousePosition.y + inputOffset - y;
			if (num < 0f)
			{
				inputOffset -= num;
				num = 0f;
			}
			if (num > mouseMaxMove)
			{
				inputOffset += mouseMaxMove - num;
				num = mouseMaxMove;
			}
			if (num <= mouseToleranceClick)
			{
				currentAngle = Mathf.Min(0f, currentAngle + speed);
				return;
			}
			float b = (num - mouseMinMove) * (maxAngle - minAngle) / ((mouseMaxMove - mouseMinMove) * 2f);
			currentAngle = Mathf.Max(0f, b);
		}
		else
		{
			inputOffset = 0f;
			startedClick = false;
			currentAngle = Mathf.Max(minAngle, currentAngle - speed);
		}
	}

	protected virtual void Update()
	{
		lastAngle = currentAngle;
		MouseInput();
		if (startedClick && !startedFallingFluidAnimation)
		{
			startedFallingFluidAnimation = true;
			liquidFallingController = UnityEngine.Object.Instantiate(prefabLiquidFalling).GetComponent<LiquidFallingController>();
			liquidFallingController.transform.position = liquidFallingController.transform.position;
			liquidFallingController.SetMaterials(backgroundFluidMaterial, foregroundFluidMaterial);
			liquidFallingController.SetBottleTransform(bottleLiquidStart.transform);
			liquidFallingController.SetGlassTransform(glassFront);
			liquidFallingController.StartAnimation();
		}
		if (startedFallingFluidAnimation && !startedClick)
		{
			startedFallingFluidAnimation = false;
			if (liquidFallingController != null)
			{
				liquidFallingController.StopAnimation();
				liquidFallingController = null;
			}
			enabledObject = false;
			StartCoroutine(DelayedSlideOutAnimation(delaySlideOutTime));
		}
		UpdateBottle();
	}

	protected void UpdateBottle()
	{
		base.transform.eulerAngles = new Vector3(0f, 0f, currentAngle);
		if (liquidFallingController != null)
		{
			float num = minFluidWith + currentAngle * (1f - minFluidWith) / maxAngle;
			if (num > 1f)
			{
				num = 1f;
			}
			liquidFallingController.SetWidth(num);
		}
		UpdateFluid();
	}

	public void SlideInAnimation()
	{
		StopAllCoroutines();
		enabledObject = true;
		slideInAnimation = true;
		currentAngle = minAngle;
		iTween.Stop(base.gameObject);
		iTween.ValueTo(base.gameObject, iTween.Hash("from", startPosition.x, "to", endPosition.x, "onupdate", "OnUpdateSlideInAnimation", "time", slideInTime, "oncomplete", "OnSliedInEnd"));
		AudioController.instance.Play("bootle_appear");
	}

	private void OnUpdateSlideInAnimation(float value)
	{
		float x = endPosition.x;
		Vector3 position = base.transform.position;
		float num = Mathf.Clamp(value, x, position.x);
		float y = endPosition.y;
		Vector3 position2 = base.transform.position;
		float num2 = Mathf.Clamp(value, y, position2.y);
		Transform transform = base.transform;
		float x2 = num;
		float y2 = num2;
		Vector3 position3 = base.transform.position;
		transform.position = new Vector3(x2, y2, position3.z);
	}

	private void OnSliedInEnd()
	{
		base.transform.position = endPosition;
	}

	private IEnumerator DelayedSlideOutAnimation(float time)
	{
		yield return new WaitForSeconds(time);
		SlideOutAnimation();
	}

	public void SlideOutAnimation()
	{
		StopAllCoroutines();
		enabledObject = false;
		slideInAnimation = false;
		iTween.Stop(base.gameObject);
		iTween.ValueTo(base.gameObject, iTween.Hash("from", endPosition.x, "to", startPosition.x, "onupdate", "OnUpdateSlideOutAnimation", "time", slideOutTime, "oncomplete", "OnSliedOutEnd"));
		if (onBottleSlideOutAction != null)
		{
			onBottleSlideOutAction();
		}
	}

	private void OnUpdateSlideOutAnimation(float value)
	{
		Vector3 position = base.transform.position;
		float num = Mathf.Clamp(value, position.x, startPosition.x);
		Vector3 position2 = base.transform.position;
		float num2 = Mathf.Clamp(value, position2.y, startPosition.y);
		Transform transform = base.transform;
		float x = num;
		float y = num2;
		Vector3 position3 = base.transform.position;
		transform.position = new Vector3(x, y, position3.z);
	}

	private void OnSliedOutEnd()
	{
		base.transform.position = startPosition;
	}

	protected void UpdateFluid()
	{
		switch (waveState)
		{
		case BottleWaveState.WAVE_START:
			MoveWave(botttleLiquidSpeedCurrentWave / divider);
			if (currentOffsetAngle == offsetAngle)
			{
				orginalOffsetAngle = offsetAngle;
				offsetAngle = 0f;
				waveState = BottleWaveState.WAVE_END;
			}
			break;
		case BottleWaveState.WAVE_END:
			MoveWave(botttleLiquidSpeedCurrentWave / divider);
			if (currentOffsetAngle == offsetAngle)
			{
				offsetAngle = orginalOffsetAngle;
				divider *= multiplierDivider;
				if (botttleLiquidSpeedCurrentWave / divider <= cuttoffAnimation)
				{
					offsetAngle = 0f;
					waveState = BottleWaveState.NULL;
				}
				else
				{
					waveState = BottleWaveState.WAVE_START;
				}
			}
			break;
		case BottleWaveState.NULL:
			MoveWave(botttleLiquidSpeedCurrentWave);
			break;
		}
		if (lastAngle != currentAngle)
		{
			float num = lastAngle - currentAngle;
			float b = (num - minAngle) * maxBottleLiquidJump / (maxAngle - minAngle);
			offsetAngle = Mathf.Max(minBottleLiquidJump, b);
			botttleLiquidSpeedCurrentWave = Mathf.Max(minLiquidSpeed, maxLiquidSpeed * Mathf.Abs(num));
			botttleLiquidSpeedCurrentWave = Mathf.Min(maxLiquidSpeed, botttleLiquidSpeedCurrentWave);
			divider = startingDivider;
			waveState = BottleWaveState.WAVE_START;
		}
		bottleLiquid.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 0f - currentAngle + currentOffsetAngle));
	}

	protected void MoveWave(float speed)
	{
		if (offsetAngle > 0f)
		{
			currentOffsetAngle = Mathf.Min(offsetAngle, currentOffsetAngle + speed);
		}
		else
		{
			currentOffsetAngle = Mathf.Max(offsetAngle, currentOffsetAngle - speed);
		}
	}

	public void SetOnBottleSlideOutAction(UnityAction action)
	{
		onBottleSlideOutAction = action;
	}

	public void SetGlassTransform(Transform glassFront)
	{
		this.glassFront = glassFront;
	}
}
